package com.njupt.swg.threadstudyjimin.item06.commonUnsafe;

import java.util.Iterator;
import java.util.Vector;

public class Test {

    //java.util.ConcurrentModificationException
    private static void iterator1(Vector<Integer> v){
        for(Integer i:v){
            if(i.equals(3)){
                v.remove(i);
            }
        }
    }

    //java.util.ConcurrentModificationException
    private static void iterator2(Vector<Integer> v){
        Iterator<Integer> iterator = v.iterator();
        while(iterator.hasNext()){
            Integer i = iterator.next();
            if(i.equals(3)){
                v.remove(i);
            }
        }
    }

    //无异常
    private static void iterator3(Vector<Integer> v){
        for(int i=0;i<v.size();i++){
            if(v.get(i).equals(3)){
                v.remove(3);
            }
        }
    }

    public static void main(String[] args) {
        Vector<Integer> vector = new Vector<>();
        vector.add(1);
        vector.add(2);
        vector.add(3);

        iterator2(vector);
    }
}
